package com.bbt.dormassistant.service.impl;

import com.bbt.dormassistant.dao.DormitoryDao;
import com.bbt.dormassistant.dao.UserInformationMapper;
import com.bbt.dormassistant.entity.Dormitory;
import com.bbt.dormassistant.entity.DormitoryDetail;
import com.bbt.dormassistant.entity.UserInformation;
import com.bbt.dormassistant.service.DormitoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DormitoryServiceImpl implements DormitoryService {

    @Resource
    private DormitoryDao dormitoryDao;

    @Resource
    private UserInformationMapper userInformationMapper;

    @Override
    public DormitoryDetail getDormitoryByUserId(String userId) {
        Dormitory dormitory = dormitoryDao.findByUserId(userId);
        List<String> roomMates = dormitory.getRoomMate();

        DormitoryDetail dormitoryDetail = new DormitoryDetail();
        dormitoryDetail.setUserId(userId);

        List<UserInformation> roomMatesList = roomMates.stream().map((id) -> {
            return userInformationMapper.selectByPrimaryKey(id);
        }).collect(Collectors.toList());

        dormitoryDetail.setRoomMates(roomMatesList);

        return dormitoryDetail;
    }

}
